package com.dodock.survey.ui;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dodock.survey.Brave;
import com.dodock.survey.R;

public class OpenList extends ListActivity {

	private EfficientAdapter efficientAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_layout);
		
		Brave.infoList = Brave.getDatabaseManager().getInformationList();
		
		Button back = (Button) findViewById(R.id.back);
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		Button export = (Button) findViewById(R.id.export);

		export.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				boolean result = Brave.getDatabaseManager()
						.exportSection2IntoCsv();
				if (result) {
					Toast.makeText(OpenList.this,
							"Data successfully exported into sd card.",
							Toast.LENGTH_LONG).show();
				}
			}
		});
		
		try {
			efficientAdapter = new EfficientAdapter(this);
			setListAdapter(efficientAdapter);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		try {
			if (efficientAdapter != null)
				efficientAdapter.notifyDataSetChanged();
			else {
				efficientAdapter = new EfficientAdapter(this);
				setListAdapter(efficientAdapter);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private class EfficientAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public EfficientAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		public int getCount() {
			if (Brave.infoList != null) {
				return Brave.infoList.size();
			}
			return 0;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			final ViewElements tagView;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.list_single_element,
						null);
				tagView = new ViewElements();
				tagView.nameText = (TextView) convertView
						.findViewById(R.id.nameOfApplicant);
				tagView.dateText = (TextView) convertView
						.findViewById(R.id.dateOfSave);
				tagView.itemNo = (TextView) convertView
						.findViewById(R.id.number);
				convertView.setTag(tagView);
			} else {
				tagView = (ViewElements) convertView.getTag();
			}

			tagView.itemNo.setText("" + (position + 1));

			int flags = DateUtils.FORMAT_ABBREV_RELATIVE;
			String name = Brave.infoList.get(position).getInfoSubjectName();
			if (name.length() > 0) {
				tagView.nameText.setVisibility(View.VISIBLE);
				tagView.nameText.setText(name);
			} else {
				tagView.nameText.setVisibility(View.GONE);
			}

			try{
				long date = Long.valueOf(Brave.infoList.get(position).getKey());
				tagView.dateText.setText(DateUtils.getRelativeTimeSpanString(date,
						System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS,
						flags));
			}catch(Exception e){
				Log.e("", e.toString());
			}
			

			return convertView;
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Intent intent = new Intent(this, SectionActivity.class);
		intent.putExtra("newForm", false);
		intent.putExtra("itemNo", position);
		startActivity(intent);
	}

	public static final class ViewElements {
		TextView nameText;
		TextView dateText;
		TextView itemNo;
	}
}
